// 获取数据 渲染到页面中
function renderSwiper() { 
    $.ajax({
        url: "admin/swipers",
        success: function (res) {
            // console.log(res);
            let html = template('tpl-table', res);
            $('tbody').html(html);
        } 
    });
};
renderSwiper();

// 轮播图状态切换
$('tbody').on('click', 'span', function (e) {
    
    let status = $(e.target).data('status');
    let id = $(e.target).data('id');
    $.ajax({
        type: "put",
        url: "admin/swipers/" + id,
        data: {
            status:status
        },
        success: function (res) {
            // console.log(res);
            if (res.status === 0) {
                layer.msg(res.message);
                renderSwiper();
            }
        }
    });
})


// 删除操作
$('tbody').on('click', '.delete', function (e) {
    
    let id = $(e.target).data('id');
    // console.log(id);
    layer.confirm('确定要删除吗?', {icon: 3, title:'提示'}, function(index){
        //do something
        $.ajax({
            type: "DELETE",
            url: "admin/swipers/" + id,
            success: function (res) {
                // console.log(res);
                if (res.status === 0) {
                    layer.msg(res.message);
                    renderSwiper();
                }
            }
        });
        layer.close(index);
    });
});


// 上传轮播图绑定上传文件按钮点击事件
// let form = layui.form;
$('#test1').on('click', function () {
    $('#myfile').click();
});
$('#myfile').change(function (e) {
    // console.log(e);
    let files = e.target.files;
    let fd = new FormData();
    $.each(files, function (index, item) {
        fd.append('swipers', item);
    });
    $.ajax({
        type: "POST",
        url: "admin/swipers",
        data: fd,
        processData: false,
        contentType: false,
        success: function (res) {
            // console.log(res);
            if (res.status === 0) {
                layer.msg(res.message);
                renderSwiper();
            }
        }
    });

});
